package com.comlan.installer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;

public class UpdatingActivity extends Activity {

	private static final String CANCEL_DIALOG = "com.comlan.tms.action.cancel_dialog";


	private Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			finish();
			Log.d("test", "finish UpdatingActivity");
			//UpdateService.setUpdatingHandler(null);
			super.handleMessage(msg);
		}
		
	};
	private Timer timer;


	private CancelDialogReceiver receiver;
	
	
	/** Called when the activity is first created. */
    @Override 
    public void onCreate(Bundle savedInstanceState) {
    	register();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updating);
    	//get display width and height.
		WindowManager.LayoutParams lp = getWindow().getAttributes();  		
	    lp.width =  350;  
	    lp.height = 250;
	    //lp.x = 20;
	    lp.gravity = Gravity.CENTER ;   
	    //lp.alpha = 0.8f;
	    getWindow().setAttributes(lp);  
        
        initViews();
    }

  
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    }
    
	private void initViews() {
	
	}
	 
	private void initData() throws Exception{
	
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if(timer != null) timer.cancel();
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				handler.sendEmptyMessage(0);
			}
		}, 1000 * 60);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unRegister();
	}
	private void unRegister() {
		if (receiver != null) {
			unregisterReceiver(receiver);
		}
	}

	private void register() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(CANCEL_DIALOG);
		receiver = new CancelDialogReceiver();
		registerReceiver(receiver, filter);
	}

	class CancelDialogReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (UpdatingActivity.CANCEL_DIALOG.equals(action)) {
				if (handler != null)
					handler.sendEmptyMessage(0);
			}

		}
	}

}
